package com.lnkj.spcj.ui.upload

import com.alibaba.sdk.android.oss.ClientException
import com.alibaba.sdk.android.oss.ServiceException
import com.alibaba.sdk.android.oss.callback.OSSCompletedCallback
import com.alibaba.sdk.android.oss.callback.OSSProgressCallback
import com.alibaba.sdk.android.oss.model.PutObjectRequest
import com.alibaba.sdk.android.oss.model.PutObjectResult
import com.lnkj.spcj.net.Net
import com.lnkj.spcj.net.UrlUtils
import com.lnkj.spcj.util.oss.OSSOperUtils
import com.lnkj.spcj.util.utilcode.FileUtils
import com.lnkj.spcj.util.utilcode.TimeUtils
import com.lnkj.spcj.util.utilcode.ToastUtils
import com.luck.picture.lib.entity.LocalMedia
import org.jetbrains.anko.runOnUiThread
import java.io.File
import java.text.SimpleDateFormat

class UploadPresent : UploadContract.Present() {
    override fun upLoadFiles(type: Int, arrayList: MutableList<LocalMedia>) {
        mView?.onUploadStart()
        var files = arrayListOf<File>()
        var urls = arrayListOf<String>()
        var isDone = arrayListOf<Boolean>()
        arrayList.forEachIndexed { index, localMedia ->
            var path = ""
            if (localMedia.isCompressed) {
                path = localMedia.compressPath
            } else {
                path = localMedia.path
            }
            if (path.contains(OSSOperUtils.AliYunOSSURLFile)) {
                urls.add(path)
                isDone.add(true)
            } else {
                var file = File(path)
                files.add(file)
                urls.add("android_videos" + "/" + TimeUtils.getNowString(SimpleDateFormat("yyyyMMddHHmmss")) + "_" + FileUtils.getFileName(file))
                isDone.add(false)
            }
        }
        arrayList.forEachIndexed { index, localMedia ->
            if (urls[index].contains(OSSOperUtils.AliYunOSSURLFile)) {
                isDone[index] = true
                if (isDone.all { it }) {
                    isDone.forEachIndexed { index, b -> isDone[index] = false }
                    mContext?.runOnUiThread {
                        mView?.onUploadFinish()
                        mView?.upLoadFiles(type, urls)
                    }
                }
            } else {
                OSSOperUtils.newInstance(mContext).putObjectMethod(urls[index], if (localMedia.compressPath.isNullOrEmpty()) localMedia.path else localMedia.compressPath, object : OSSProgressCallback<PutObjectRequest> {
                    override fun onProgress(request: PutObjectRequest?, currentSize: Long, totalSize: Long) {
                        mContext?.runOnUiThread {
                            mView?.onUploadProgress("${currentSize.times(100).div(totalSize)}%")
                        }
                    }
                }, object : OSSCompletedCallback<PutObjectRequest, PutObjectResult> {
                    override fun onSuccess(request: PutObjectRequest?, result: PutObjectResult?) {
                        isDone[index] = true
                        if (isDone.all { it }) {
                            isDone.forEachIndexed { index, b -> isDone[index] = false }
                            mContext?.runOnUiThread {
                                mView?.onUploadFinish()
                                mView?.upLoadFiles(type, urls)
                            }
                        }
                    }

                    override fun onFailure(request: PutObjectRequest?, clientException: ClientException?, serviceException: ServiceException?) {
                        isDone[index] = true
                        if (isDone.all { it }) {
                            isDone.forEachIndexed { index, b -> isDone[index] = false }
                            mContext?.runOnUiThread {
                                mView?.onUploadFinish()
                                mView?.upLoadFiles(type, urls)
                            }
                        }
                    }
                })
            }
        }
    }

    override fun saveVideo(video_url: String, station_id: String, company_id: String, department_id: String) {
        Net.post(mContext, UrlUtils().saveVideo, mapOf("video_url" to video_url,
        "station_id" to station_id,
        "company_id" to company_id,
        "department_id" to department_id), object : Net.Callback(mContext, true) {
            override fun onSuccess(t: Any?) {
                mView?.saveVideo()
            }

            override fun onError(apiException: Throwable?) {
                mView?.onError()
            }

            override fun onMessage(status: Int, msg: String) {
                super.onMessage(status, msg)
                ToastUtils.showShort(msg)
            }
        })
    }
}